from sklearn.svm import OneClassSVM
import numpy as np
import joblib

class CustomOneClassSVM(OneClassSVM):
    def __init__(self, nu=0.5, kernel='rbf', degree=3, gamma='scale', coef0=0.0, tol=1e-3, shrinking=True, 
                 cache_size=200, verbose=False, max_iter=-1):
        super().__init__(nu=nu, kernel=kernel, degree=degree, gamma=gamma, coef0=coef0, tol=tol, 
                         shrinking=shrinking, cache_size=cache_size, verbose=verbose, max_iter=max_iter)

if __name__ == '__main__':
    # 训练数据
    X = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [10, 10], [11, 11], [12, 12]])
    oc_svm = CustomOneClassSVM(nu=0.1, kernel='rbf', gamma=0.1)
    oc_svm.fit(X)

    # 序列化模型为 joblib 文件
    joblib.dump(oc_svm, 'oc_svm.joblib')

    # 反序列化模型
    loaded_oc_svm = joblib.load('oc_svm.joblib')

    # 预测新数据
    X_test = np.array([[1, 1], [3, 3], [11, 11], [13, 13]])
    y_pred = loaded_oc_svm.predict(X_test)
    print("预测结果:", y_pred)